package com.rimi.dao.impl;

import com.rimi.bean.UserBean;
import com.rimi.dao.UserDao;
import com.rimi.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jerry
 */
public class UserDaoImpl extends JdbcUtil implements UserDao {

    private UserBean parseResultSetToUser(ResultSet resultSet) throws SQLException {
        UserBean userBean=new UserBean();
        userBean.setUserId(resultSet.getInt(1));
        userBean.setUserEmail(resultSet.getString(2));
        userBean.setUserPassword(resultSet.getString(3));
        userBean.setUserHeadImg(resultSet.getString(4));
        userBean.setUserStatus(resultSet.getInt(5));
        userBean.setUserRole(resultSet.getInt(6));
        return userBean;
    }

    @Override
    public List<UserBean> selectUserList() {
        String sql="select * from tb_user";
        ResultSet resultSet = executeQuerySql(sql);
        List<UserBean> userBeanList=new ArrayList<>();
        try {
            while (resultSet.next()){
                userBeanList.add(parseResultSetToUser(resultSet));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeRes();
        }
        return userBeanList;
    }

    @Override
    public boolean addUser(UserBean userBean) {
        return false;
    }

    @Override
    public boolean updateUserById(UserBean userBean) {
        if(userBean == null){
            return false;
        }
        String sql="update tb_user set user_head_img=? , user_status = ?,user_role = ? where user_id = ?";
        Object[] params={userBean.getUserHeadImg(),userBean.getUserStatus(),userBean.getUserRole(),userBean.getUserId()};
        return executeUpdateSql(sql,params);
    }

    @Override
    public boolean deleteUserById(Integer id) {
        return executeUpdateSql("delete from tb_user where user_id = ?",id);
    }

    @Override
    public boolean deleteUserByIds(List<Integer> ids) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(" delete from tb_user where  user_id  in(");
        for (int i = 0; i < ids.size(); i++) {
            //如果是最后一个
            if(i>= ids.size()-1){
                stringBuffer.append(ids.get(i));
                continue;
            }
            stringBuffer.append(ids.get(i)+",");
        }
        stringBuffer.append(")");
        // user_id in(1,2,3,4,5,6,7,7);
        return executeUpdateSql(stringBuffer.toString());
    }

    @Override
    public UserBean selectUserByEmail(String email) {
        String sql="select * from tb_user where user_email = ?";
        Object[] params={email};
        ResultSet resultSet = executeQuerySql(sql, params);
        try {
            if(resultSet.next()){
                return parseResultSetToUser(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeRes();
        }
        return null;
    }

    @Override
    public UserBean selectUserById(Integer id) {
        String sql="select * from tb_user where id = ?";
        Object[] params={id};
        ResultSet resultSet = executeQuerySql(sql, params);
        try {
            if(resultSet.next()){
                return parseResultSetToUser(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeRes();
        }
        return null;
    }

    @Override
    public UserBean selectUserByToken(String token) {
        String sql="select * from tb_user where user_token = ? and now() < user_token_end_time";
        Object[] params={token};
        ResultSet resultSet = executeQuerySql(sql, params);
        try {
            if(resultSet.next()){
                return parseResultSetToUser(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeRes();
        }
        return null;
    }

    @Override
    public boolean addTokenById(Integer userId, String token) {
        LocalDateTime localDateTime = LocalDateTime.now().plusDays(7);
        localDateTime=LocalDateTime.now().plusMinutes(1);
        String sql="update tb_user set user_token = ?,user_token_end_time=? where user_id = ?";
        Object[] params={token,localDateTime,userId};
        return executeUpdateSql(sql, params);
    }

    @Override
    public boolean updateTokenByUserId(Integer userId) {
        String sql="update tb_user set user_token =?,user_token_end_time =? where user_id = ?";
        Object[] params={"",LocalDateTime.now().minusMinutes(1),userId};
        return executeUpdateSql(sql,params);
    }

}
